package multithreading;

import java.util.Random;

public class Demo2_DownloadFile {


    public static void main(String[] args) {

        //创建四个线程，在一个进程中同时执行
        Thread thr_first = new Download("java从基础到精通");
        Thread thr_second = new Download("颈椎病的预防");
        Thread thr_third = new Download("高血压的诊断");
        Thread thr_fourth = new Download("活着");

        //必须得开启线程才能执行
        thr_first.start();
        thr_second.start();
        thr_third.start();
        thr_fourth.start();
    }

}

/**
 * 创建一个模拟迅雷下载的线程类
 * 创建一个当前类的实例化对象就相当于创建了一个线程
 */
class Download extends Thread{

    //下载的文件名
    String fileName = "";

    Random rand = new Random();


    public Download(String fileName){
        this.fileName = fileName;
    }

    //每一个线程需要执行的内容
    @Override
    public void run() {
        try {
            for (int i = 1; i <= 100; i++) {
                //每个文件都需要从 1% 下载到 100%
                System.out.println(this.fileName + "已经下载了：" + i + "%");
                //为了避免执行速度过快，对线程加了一些休眠时间
                Thread.sleep(rand.nextInt(1000));
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
